﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.UC.抖音出入库.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.抖音出入库.抖音下单
{
    public partial class Form抖音下单 : DevExpress.XtraEditors.XtraForm
    {
        //  地址
        private List<DouyinCustomerAddressDao> douyinCustomerAddressDaos;
        private DouyinCustomerAddressDao douyinCustomerAddressDao = new DouyinCustomerAddressDao();
        //  
        public Form抖音下单(String mobile,String customerName)
        {
            InitializeComponent();
            this.textBox客户名.Text = customerName;
            this.textBox电话.Text = mobile;
            //  客户地址一览
            this.douyinCustomerAddressDaos = 抖音Service.getCustomerAddressLog(mobile);
            this.searchLookUpEdit地址.Properties.DataSource = this.douyinCustomerAddressDaos;
            //  默认选中第一条地址
            this.默认地址();
        }

        #region 选择地址
        private void 默认地址()
        {
            if (this.douyinCustomerAddressDaos.Count > 0)
            {
                this.douyinCustomerAddressDao = this.douyinCustomerAddressDaos[0];
                this.显示位置图();
                this.searchLookUpEdit地址.Text = this.douyinCustomerAddressDao.formattedAddress + this.douyinCustomerAddressDao.addressNumber;
            }
        }

        private void searchLookUpEdit地址_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear审版选择员工Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit地址View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit地址.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    String userId = this.searchLookUpEdit地址.Properties.View.GetRowCellValue(rowHandle, "userId").ToString();
                    //  取选中地址
                    foreach (DouyinCustomerAddressDao item in this.douyinCustomerAddressDaos)
                    {
                        if (userId.Equals(item.userId))
                        {
                            this.douyinCustomerAddressDao = item;
                            this.显示位置图();
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear审版选择员工Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit地址.ToolTip = null;
            searchLookUpEdit地址.EditValue = null;
        }
        private void searchLookUpEdit地址_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.douyinCustomerAddressDao.formattedAddress + this.douyinCustomerAddressDao.addressNumber;
        }

        private void 显示位置图()
        {
            try
            {
                //显示图片
                this.pictureBox定位图.LoadAsync("https://restapi.amap.com/v3/staticmap?location=" + this.douyinCustomerAddressDao.location + "&zoom=10&size=750*300&markers=mid,,A:" + this.douyinCustomerAddressDao.location + "&key=1e35c2196190d3f06060fb90f1c4ce30");
            }
            catch { }
        }
        #endregion

    }
}